package com.axon.dataservices {
  import com.axon.dataservices.helpers.AxDSAsyncResponder;

  import mx.core.Application;

  import org.restfulx.Rx;
  import org.restfulx.controllers.AuxHTTPController;
  import org.restfulx.controllers.RxApplicationController;
  import org.restfulx.serializers.ISerializer;

  public class AxDS {
    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public static const GET:int = AuxHTTPController.GET;

    public static const POST:int = AuxHTTPController.POST;

    public static const PUT:int = AuxHTTPController.PUT;

    public static const DELETE:int = AuxHTTPController.DELETE;

    private static var sRxApplicationController:RxApplicationController;

    public static var timeout:int = 3000;

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    static public function initialize(commands:Array, models:Array, extraServices:Array = null, targetServiceId:int = -1):void {
      Rx.enableLogging();
      authenticityToken = Application.application.parameters.authenticity_token;
      var rootUrl:String = Application.application.parameters.http_root_url;
      if (rootUrl) httpRootUrl = rootUrl;
      sRxApplicationController = new RxApplicationController(commands, models, extraServices, targetServiceId);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    static public function httpInvoke(
      onSuccess:Object,
      onFault:Function,
      optsOrURL:Object,
      data:Object = null,
      method:* = 1,
      unmarshall:Boolean = false,
      contentType:String = "application/x-www-form-urlencoded",
      resultFormat:String = "e4x",
      serializer:ISerializer = null,
      rootUrl:String = null,
      cacheBy:String = null):void {
      var responder:AxDSAsyncResponder = new AxDSAsyncResponder(onSuccess as Function, onFault);

      Rx.http(responder.handleSuccess, responder.handleFailure, contentType,
        resultFormat, serializer, rootUrl).invoke(optsOrURL, data,
        method, unmarshall, cacheBy);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public static function reload(object:Object,
      onSuccess:Object = null,
      onFailure:Function = null,
      nestedBy:Array = null,
      metadata:Object = null,
      fetchDependencies:Boolean = true,
      useLazyMode:Boolean = true,
      append:Boolean = false,
      targetServiceId:int = -1):void {
      var responder:AxDSAsyncResponder = new AxDSAsyncResponder(onSuccess as Function, onFailure);

      Rx.models.reload(object, responder.handleSuccess, responder.handleFailure, nestedBy, metadata,
        fetchDependencies, useLazyMode, append, targetServiceId);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public static function update(object:Object,
      onSuccess:Object = null,
      onFailure:Function = null,
      nestedBy:Array = null,
      metadata:Object = null,
      recursive:Boolean = false,
      targetServiceId:int = -1):void {
      var responder:AxDSAsyncResponder = new AxDSAsyncResponder(onSuccess as Function, onFailure);

      Rx.models.update(object, onSuccess, onFailure, nestedBy, metadata, recursive, targetServiceId);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public static function set sessionToken(value:String):void {
      Rx.sessionToken = value;
    }

    static public function set authenticityToken(value:String):void {
      Rx.authenticityToken = value;
    }

    static public function set httpRootUrl(value:String):void {
      Rx.httpRootUrl = value;
    }

  }
}